<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
$_SESSION['navigation'] = 'arbitrages';
$user_id = check_cookie();
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "list";
$match_no = (!empty($_REQUEST['match_no'])) ? $_REQUEST['match_no'] : "";
if($action == 'update'){
	$option = (!empty($_REQUEST['option'])) ? $_REQUEST['option'] : "prochains";
	$_SESSION['affichage'] = $option;
}
elseif($action == 'detail'){
	$match = new arbitrage($match_no);
	$salle = new salle($match->salle);
	$date = format_date($match->date);
	$heure = format_heure($match->heure);
	$fields = describe('arbitrages');
	echo "<div>\n";
	echo "<h2>arbitrage $match_no</h2>";
	foreach($fields as $field => $type){
		echo leftCol("<span class = 'bold'>$field</span>");
		if($field == 'date'){
			$print = $date;
		}
		elseif($field == 'heure'){
			$print = $heure;
		}
		elseif($field == 'salle'){
			$print = $salle->lien();
		}
		elseif(strpos($field,'arbitre') !== FALSE){
			$membre = new membre($match->$field);
			$print = $membre->prenom." ".$membre->nom;
		}
		else $print = $match->$field;
		echo rightCol($print);
	}
	echo "<br class = 'clear'/>\n";
	if($match->date >= date('Y-m-d')){
		echo "<h2>autres arbitres disponibles</h2>\n";
		$bonnes = array();
		$q = "select distinct(equipe) from matchs where date = '$match->date' and salle = '$match->salle'";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$equipe = new equipe($s->equipe,"nom");
			$bonnes[$equipe->id] = $s->equipe;
		}
		$q = "select equipe_id,nom from entrainements left join equipes on equipe_id = equipes.id where jour = '$match->jour' and salle = '$match->salle'";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$bonnes[$s->equipe_id] = $s->nom;
		}
		$equipes = liste('equipes');
		$q = "select distinct(equipe) from matchs where date = '$match->date' and salle != '$match->salle'";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			if(isset($equipes)){
				foreach($equipes as $id => $nom){
					if($nom == $s->equipe){unset($equipes[$id]); }
				}
			}
		}
		if(isset($equipes)){
			$q = "select equipe_id,nom from entrainements left join equipes on equipe_id = equipes.id where jour = '$match->jour' and salle != '$match->salle'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				if(isset($equipes)){
					foreach($equipes as $id => $nom){
						if($nom == $s->nom){unset($equipes[$id]); }
					}
				}
			}
		}
		$categories = array("Bonnes disponibilit&eacute;s" => "bonnes", "Disponibles" => "equipes");
		foreach($categories as $titre => $table){
			echo "<p class = 'bold center'>$titre</p>";
			$i = 0;
			${"print_membres_".$table}= array();
			if(isset($$table)){
				foreach($$table as $id => $nom){
					$equipe = new equipe($id);
					$membres = $equipe->list_membres();
					foreach($membres as $membre_id => $membre){
						$ok = (($membre->arbitre) || (strstr($match->ligue,"M16") !== FALSE && $membre->arbitre_junior)) ? 1 : 0;						if($ok){
							foreach($membre->arbitrages as $marq_id => $marq_no){
								$arbitrage = new arbitrage($marq_no);
								if(($arbitrage->date == $match->date) && ($arbitrage->salle != $match->salle || $arbitrage->no != $match->no)) $ok = 0;
							}
							foreach($membre->marquages as $marq_id => $marq_no){
								$marquage = new match($marq_no);
								if($marquage->date == $match->date) $ok = 0;
							}
						}
						if($ok){
							$junior = ($membre->arbitre_junior && !$membre->arbitre) ? "[J]&nbsp;" : "";
							${"print_membres_".$table}[$membre->id] = $junior.$membre->prenom." ".$membre->nom." ($membre->nb_arbitrages)";
							if($membre->nb_arbitrages == "0" || $membre->nb_arbitrages == "1" || $membre->nb_arbitrages == "2" || $membre->nb_arbitrages == "3") ${"print_membres_".$table}[$membre->id] = "<span class = 'highlight'>".${"print_membres_".$table}[$membre->id]."</span>";
						}
					}
				}
				asort(${"print_membres_".$table});
				foreach(${"print_membres_".$table} as $membre_id => $print){
					if($table == 'bonnes' || !array_key_exists($membre_id,$print_membres_bonnes)){
						$echo = "<span style = 'cursor: pointer' onclick = 'show_membre($membre_id,\"$match_no\")'>$print</span>";
						if(++$i % 2) echo leftCol($echo);
						else echo rightCol($echo);
					}
				}
				if(!count(${"print_membres_".$table})) echo "<p class = 'center fade'>aucune</p>\n";
				if($i % 2) echo rightCol("");
				echo "<br class = 'clear' />";
			}
		}
	}
	else echo "<p class = 'center fade'>La date de l'arbitrage est pass&eacute;e</p>\n";
	echo "</div>\n";
}
elseif($action == 'confirm'){
	$match_no = (!empty($_REQUEST['match_no'])) ? $_REQUEST['match_no'] : "";
	$match = new arbitrage($match_no);
	$salle = new salle($match->salle);
	$date = format_date($match->date);
	$heure = format_heure($match->heure);
	$membre = new membre($user_id);
	$matchs = $membre->matchs();
	$message = "";
	foreach($membre->marquages as $match){
		if(format_date($match->date) == $date){
			$message .= "<p class = 'center bold'>!! Attention, vous avez un marquage le $date !!</p>";
		}
	}
	foreach($membre->arbitrages as $match){
		if(format_date($match->date) == $date){
			$message .= "<p class = 'center bold'>!! Attention, vous avez un arbitrage le $date !!</p>";
		}
	}

	if(!$message){
		foreach($matchs as $match){
			if(format_date($match->date) == $date){
				$message .= "<p class = 'center bold'>!! Attention, vous avez un match le $date !!</p>";
			}
		}
	}
	if($message) echo $message;
	echo "<p class = 'center bold'>Confirmer l'inscription &agrave; l'arbitrage du $date &agrave; $heure &agrave; $salle->nom</p>";
	echo "<p class = 'center'><button onclick = 'add_arbitrage(\"$match_no\")'>confirmer l'inscription</button><button onclick = 'center_box(0);'>annuler</button></p>";
}
elseif($action == 'add'){
	if($user_id){
		$match = new arbitrage($match_no);
		$membre = new membre($user_id);
		if(($membre->arbitre) || (strstr($match->ligue,"M16") !== FALSE && $membre->arbitre_junior)){
			if($match->arbitre1){
				$membre = new membre($match->arbitre1);
				$ddns = explode("-",$membre->ddn);
				$year = $ddns[0];
				if((date('Y') - $year >= 18) || ($membre->arbitre_junior && strstr($match->ligue,"M16") !== FALSE)){
					$match->arbitre2 = $user_id;
					$match->update();
					mail_arbitrage($match->no);
					echo "Inscription r&eacute;ussie. Un email de confirmation vous a &eacute;t&eacute; envoy&eacute;.";
				}
				else echo "Vous ne pouvez pas vous inscrire &agrave; cet arbitrage";
			}
			else{
				$match->arbitre1 = $user_id;
				$match->update();
				mail_arbitrage($match->no);
				echo "Inscription r&eacute;ussie. Un email de confirmation vous a &eacute;t&eacute; envoy&eacute;.";
			}
		}
		else echo "Vous devez &ecirc;tre arbitre pour vous inscrire";
	}
	else echo "Vous ne pouvez pas vous inscrire &agrave; cet arbitrage";
}
elseif($action == 'print'){
	$affichage = (!empty($_SESSION['affichage'])) ? $_SESSION['affichage'] : "";
	$table = new table();
	$table->filter = 1;
	$table->order = 'date';
	$table->styleclass = '';
	$fields = array("no","ligue","jour","date","heure","salle","arbitre 1", "arbitre 2", "disp","info");
	$logged_membre = new membre($user_id);
	foreach($fields as $field){
		$$field = new table_field($field);
		if($field == 'no') $no->hidden = 1;
		elseif(strpos($field,"arb")!== FALSE) $$field->styleclass = 'nowrap';
		elseif($field == 'date' || $field == 'jour' || $field == 'heure') $$field->styleclass = 'center';
		elseif($field == 'info') $info->styleclass = 'nowrap';
		elseif($field == 'disp'){
			$disp->styleclass = 'disp';
			if(!check_cookie()) $disp->hidden = 1;
			elseif(!$logged_membre->arbitre && !$logged_membre->arbitre_junior) $disp->hidden =1;
		}

		$table->fields[] = $$field;
	}
	$id->hidden = 1;
	$table->nb_rows = 30;
	$matchs = liste('arbitrages',"no","no");
	$i = 0;
	if($user_id && ($logged_membre->arbitre || $logged_membre->arbitre_junior)){
		$membre_dates = $logged_membre->dates();
		$logged_dates = array();
		foreach($membre_dates as $dateheure => $salle){
			$tmp_array = explode("<>",$dateheure);
			$logged_dates[$tmp_array[0]] = $tmp_array[1];
		}
		$logged_salles = array();
		$logged_jours = array();
		foreach($logged_membre->equipes as $eq_id => $eq_nom){
			$q = "select * from entrainements where equipe_id = '$eq_id' and `match` = 0";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$logged_salles[$s->id] = $s->salle;
				$logged_jours[$s->id] = $s->jour;
			}
		}
	}
	$table->nb_rows = count($matchs);
	foreach($matchs as $arbitrage){
		$match = new arbitrage($arbitrage);
		if((($affichage == "prochains" && $match->date >= date('Y-m-d')) ||
			($affichage == "arbitrage" && !$match->arbitre1) ||
			($affichage == "tous"))){
				$row = new table_row();
				$row->onclick = "";
				foreach($fields as $field){
					if($field == 'salle'){
						$salle = new salle($match->$field);
						$echo = $salle->lien();
					}
					elseif($field == 'info'){
						$caution = ($match->arbitre1 && ($match->arbitre2 || strpos($match->ligue,"4") !== FALSE || strpos($match->ligue,"M18") !== FALSE || strpos($match->ligue,"M16") !== FALSE|| strpos($match->ligue,"M19") !== FALSE) || ereg("M2[123][FG]2",$match->ligue) !== FALSE) ? "" : "<img src = 'images/icon_warning.png' alt = '!!' height= '14' width = '14' />";
						$echo = "<a href = 'javascript:detail_arbitrage(\"$match->no\")'><img src = 'images/icon_info.png' alt = 'i' height = '14' width = '14'  /></a>$caution";
					}
					elseif($field == 'disp'){
						if($match->date >= date('Y-m-d')){
							if($user_id){
								if($logged_membre->arbitre || (strstr($match->ligue,"M16") !== FALSE && $logged_membre->arbitre_junior)){
									$ok = 1;
									if($match->arbitre2) $ok = 0;
									elseif($match->arbitre1 && (strpos($match->ligue,"4") !== FALSE || strpos($match->ligue,"M18") !== FALSE || strpos($match->ligue,"M19") !== FALSE) || ereg("M21[FG]2",$match->ligue) !== FALSE)	$ok = 0;
									if($ok){
										if(array_key_exists(format_date($match->date),$logged_dates)){
											$key = format_date($match->date);
											if(($membre_dates[$key."<>".$logged_dates[$key]] == $match->salle) && (array_search($match->equipe,$logged_membre->equipes) === FALSE)) $echo =  "<img src = 'images/icon_ok.png' alt = '++' height = '16' width = '16' />";
											else $echo =  "<img src = 'images/icon_cancel.png' alt = '--' height = '14' width = '14' />";
										}
										elseif(array_search($match->equipe,$logged_membre->equipes)) $echo =  "<img src = 'images/icon_delete.png' alt = '--' height = '14' width = '14' />";
										else $echo =  "<img src = 'images/icon_ok.png' alt = '--' height = '14' width = '14' />";
									}
									else $echo = "";
								}
								else $echo = "";
							}
							else $echo = "";
						}
						else $echo = "";
					}
					elseif($field == 'arbitre 1'){
						if(!$match->arbitre1){
							$ok = 1;
							if($match->date < date('Y-m-d')) $ok = 0;
							if($ok){
								$ok = (($logged_membre->arbitre) || (strstr($match->ligue,"M16") !== FALSE && $logged_membre->arbitre_junior)) ? $ok : 0;
								if($user_id && $ok) $echo = "<button style = 'font-size: 7pt' type = 'button' onclick = 'confirm_arbitrage(\"".$match->no."\")'>je m'inscris</button>";
								else $echo = "";
							}
							else $echo = "";
						}
						else{
							$membre = new membre($match->arbitre1);
							$echo = $membre->prenom." ".$membre->nom;
						}
					}
					elseif($field == 'arbitre 2'){
						if(!$match->arbitre2){
							$arbitre1 = new membre($match->arbitre1);
							if((strpos($match->ligue,"4") !== FALSE)||(strpos($match->ligue,"M18") !== FALSE)|| strpos($match->ligue,"M19") !== FALSE  || ereg("M2[123][FG]2",$match->ligue) !== FALSE  || (strpos($match->ligue,"M16") !== FALSE && $arbitre1->arbitre)){
								$ok = 2;
								$echo = "<span class = 'fade'>pas n&eacute;cessaire</span>";
							}
							else $ok = 1;
							if($match->date < date('Y-m-d') || (!$match->arbitre1 && $ok != 2)) $ok = 0;
							if($ok == 1){
								$ok = (($logged_membre->arbitre) || (strstr($match->ligue,"M16") !== FALSE && $logged_membre->arbitre_junior)) ? $ok : 0;
								if($user_id && $ok) $echo = "<button style = 'font-size: 7pt' type = 'button' onclick = 'confirm_arbitrage(\"".$match->no."\")'>je m'inscris</button>";
								else $echo = "";
							}
							elseif($ok != 2) $echo = "";
						}
						else{
							$membre = new membre($match->arbitre2);
							$echo = $membre->prenom." ".$membre->nom;
						}
					}
					else $echo = str_ireplace("vbc ","",$match->$field);
					$row->cols[$field] = $echo;
				}
				$key = key($table->rows) + 1;
				$table->rows[] = $row;
				$i++;
		}
	}
	$footer = "";
	if($user_id && ($logged_membre->arbitre || $logged_membre->arbitre_junior)){
		echo "<p class= 'fade' style = 'padding-left: 20px'><span class = 'bold'>disp. = vos disponibilit&eacute;s</span><br /><img src = 'images/icon_ok.png' alt = '++' height = '14' width = '14' /> : arbitrage favorable (en dehors de vos entrainements et matchs)<br /><img src = 'images/icon_delete.png' alt = '--' height = '14' width = '14' /> : arbitrage d&eacute;favorable (en m&ecirc;me temps que vos entrainements ou matchs)</p>\n";
	}
	elseif(!$user_id){
		echo "<p class = 'fade center'>Identifiez-vous pour vous inscrire &agrave; un arbitrage</p>\n";
	}

	echo $table->print_header();
	echo $table->print_body();
	$table->print_footer($footer);
}
else{
	entete('arbitrages');
	$_SESSION['affichage'] = 'prochains';
	$affichages = array("prochains" => "prochains arbitrages","tous" => "tous les arbitrages", "arbitrage" => "matchs sans arbitre");

	if($action == 'liste'){
		echo "<h1>Liste des arbitres</h1>\n";
		echo "<div class = 'hidden' style = 'margin-left: 60px; height: 150px;display: none; visibility: hidden' id = 'membre_details'>\n";
		box("<div id = 'nom'></div>","<div id = 'details'></div>",500,1);
		echo "</div>\n";
		$table = new table();
		$fields = array("prenom" => "pr&eacute;nom",
						"nom" => "nom",
						"tel" => "t&eacute;l&eacute;phones",
						"email" => "email",
						"nb" => "nb",
						"cat" => "cat.");
		foreach($fields as $field => $print){
			$$field = new table_field($field);
			$$field->print = $print;
			if(($field == 'nb')||($field == 'cat')) $$field->styleclass = 'center';
			$table->fields[] = $$field;
		}
		$nb->styleclass = 'center';
		$table->filter = 1;
		$table->styleclass = 'bold_hover';
		$table->order = 'nom';
		$q = "select membres.id as id,
				prenom,
		 		membres.nom as nom,
		 		concat(tel,'<br />',mobile) as tel,
				email,
				count(arbitrages.id) as nb,
				arbitre_junior
				from membres
				left join arbitrages on (membres.id = arbitre1 or membres.id = arbitre2)
				where (arbitre = 1 or arbitre_junior = 1) and valide = 1 and fictif = 0
				group by membres.id
				order by nom, prenom";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$row = new table_row();
			$row->onclick = "onclick = \"membre_details($s->id)\"";
			foreach($fields as $field => $print){
				$echo = "";
				if($field == 'email') $echo = print_email($s->email);
				elseif($field == 'cat') $echo = ($s->arbitre_junior) ? "jun." : "";
				else $echo = $s->$field;
				$row->cols[$field] = $echo;
			}
			$table->rows[] = $row;
		}
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();
		echo "<p class = 'center fade'><span class = 'bold'>jun.</span> = arbitre junior</p>\n";
	}
	else{
		echo "<h1>Calendrier des arbitrages</h1>";
		echo "<p class = 'center bold'>options d'affichage : ";
		echo "<select name = 'affichage' onchange = 'update_arbitrages(this.options[this.selectedIndex].value)'>\n";
		foreach($affichages as $option => $print){
			$selected = ($_SESSION['affichage'] == $option) ? "selected = 'selected'" : "";
			echo "<option value = '$option' $selected>$print</option>\n";
		}
		echo "</select>\n";
		echo "</p>";
		echo "<div id = 'arbitrages'>\n";
		echo "</div>\n";
		echo "<script type = 'text/javascript'>print_arbitrages()</script>\n";
	}
	pied();
}

?>
